package com.ruoyi.oa.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaProject;
import com.ruoyi.oa.domain.OaPurchaseParity;
import com.ruoyi.oa.domain.bo.OaPurchaseParityBo;
import com.ruoyi.oa.domain.vo.OaPurchaseContactVo;
import com.ruoyi.oa.domain.vo.OaPurchaseParityVo;
import com.ruoyi.oa.service.IOaPurchaseParityService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 比价信息
 *
 * @author ruoyi
 * @date 2023-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/purchaseParity")
public class OaPurchaseParityController extends BaseController {

    private final IOaPurchaseParityService iOaPurchaseParityService;

    /**
     * 查询比价信息列表
     */
//    @SaCheckPermission("oa:purchaseParity:list")
    @GetMapping("/list")
    public TableDataInfo<OaPurchaseParityVo> list(OaPurchaseParityBo bo, PageQuery pageQuery) {
        return iOaPurchaseParityService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出比价信息列表
     */
//    @SaCheckPermission("oa:purchaseParity:export")
    @Log(title = "比价信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaPurchaseParityBo bo, HttpServletResponse response) {
        List<OaPurchaseParityVo> list = iOaPurchaseParityService.queryList(bo);
        ExcelUtil.exportExcel(list, "比价信息", OaPurchaseParityVo.class, response);
    }

    /**
     * 获取比价信息详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:purchaseParity:query")
    @GetMapping("/{id}")
    public R<OaPurchaseParityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaPurchaseParityService.queryById(id));
    }

    /**
     * 通过项目名称查询采购申请
     * @param projectName
     * @return
     */
    @RequestMapping("/queryByProjectName")
    public R<OaPurchaseParityVo> queryByProjectName(@RequestBody Map<String,Object> map) {
        return R.ok(iOaPurchaseParityService.queryByProjectName(map.get("projectName").toString()));
    }

    /**
     * 新增比价信息
     */
//    @SaCheckPermission("oa:purchaseParity:add")
    @Log(title = "比价信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaPurchaseParityBo> add(@Validated(AddGroup.class) @RequestBody OaPurchaseParityBo bo) {
        OaPurchaseParityVo queryByProjectName = iOaPurchaseParityService.queryByProjectName(bo.getProjectName());
        if (ObjectUtil.isNotEmpty(queryByProjectName)){
            return R.fail(bo.getProjectName()+"已经存在,不能重复申请.");
        }
        return R.ok(iOaPurchaseParityService.insertByBo(bo));
    }

    /**
     * 修改比价信息
     */
//    @SaCheckPermission("oa:purchaseParity:edit")
    @Log(title = "比价信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaPurchaseParity> edit(@Validated(EditGroup.class) @RequestBody OaPurchaseParityBo bo) {
        return R.ok(iOaPurchaseParityService.updateByBo(bo));
    }

    /**
     * 删除比价信息
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:purchaseParity:remove")
    @Log(title = "比价信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaPurchaseParityService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @Log(title = "获取没有比价信息的项目信息")
    @GetMapping("/getProjectList")
    public R<List<OaProject>> getProjectList() {
        return R.ok(iOaPurchaseParityService.getProjectList());
    }
}
